package com.mihu.stmall.front.mapper;

import com.mihu.stmall.front.entity.Goods;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface GoodsMapper {
    //根据分类 name关键字进行查询

    //排序  价格   销量   综合（不排序）
    @Select({
            "<script>",
            "select * from goods",
            "inner join category on goods.category_id = category.id",
            "inner join category as p on category.parent_id = p.id",
            "<where>",
            "<if test='where.name !=null and where.name.length>0'> goods.name like'%${where.name}%'</if>",
            "<if test='where.categoryId !=null'>and",
            "category_id=#{where.categoryId} or category.parent_id=#{where.categoryId}",
            "</if>",
            "and goods.status = 0",//商品必须是上架状态
            "and category.status = 0",//二级分类必须是上架状态
            "and p.status = 0", //一级分类必须是上架状态
            "</where>",
            "order by ${orderBy}",
            "</script>",
    })
    @Results(id="goods",value = {

            @Result(column = "id",property = "id",id = true),
            //关联图片
            @Result(column = "id",property = "pics",
            many=@Many(select = "com.mihu.stmall.front.mapper.GoodsPicMapper.selectByGoodsId"))

    })
    List<Goods> select(@Param("where") Goods where, @Param("orderBy") String orderBy);

    //根据ID查询
    @Select("select * from goods where id=#{id}")
    @ResultMap("goods")
    Goods selectById(Integer id);
}
